package com.frxuexi.park2301.user

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.mvvm.BaseActivity
import com.frxuexi.lib_base.net.Const
import com.frxuexi.park2301.R
import com.frxuexi.park2301.databinding.ActivityLoginBinding
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/app/LoginActivity")
@AndroidEntryPoint
class LoginActivity : BaseActivity<ActivityLoginBinding,UserViewModel>() {
    override fun initData() {
        vm.loginData.observe(this){
            Log.i("lr",it.toString())
            SPUtils.getInstance().apply {
                put(Const.TOKEN,it.token)
                put(Const.UID,it.id)
                put(Const.USERNAME,it.username)
                put(Const.ROLE,it.roleid)
                put("loginTime",System.currentTimeMillis())
            }
            start()
        }
    }

    override fun initView() {
        val loginTime = SPUtils.getInstance().getLong("loginTime", 0)
        val currentTime = System.currentTimeMillis()
        val day=(currentTime-loginTime)/1000/60/60/24
        if(day<7){
            start()
        }
        binding.btnLogin.setOnClickListener {
            val uname = binding.loginUname.text.toString()
            val pass = binding.loginPass.text.toString()
            if(uname.isNullOrEmpty() || pass.isNullOrEmpty()){
                ToastUtils.showLong("输入内容不能为空")
                return@setOnClickListener
            }
//            1[0-9]{10}
            if (!uname.matches(Regex("[A-z0-9_]{2,8}"))){
                ToastUtils.showLong("不是合法用户名")
                return@setOnClickListener
            }
            vm.login(uname,pass)
        }
    }

    fun start(){
        ARouter.getInstance().build("/app/MainActivity").navigation()
//        if(SPUtils.getInstance().getInt(Const.ROLE,-1)==11) {
//            ARouter.getInstance().build("/app/MainActivity").navigation()
//        }else{
//            ARouter.getInstance().build("/app/WebViewActivity").navigation()
//        }
        finish()
    }

}